<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>合同模板编辑</title>
 <%@ include file="/WEB-INF/component/commonCSS.jsp" %> 
  <style type="text/css">
    	div.box-body {
    		margin-top:7px;
    	}
    </style>
  
</head>
<body class="skin-blue sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
        <h1>合同编辑</h1>
        <ol class="breadcrumb">
            <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i>首页</a></li>
            <li class="active"><a href="#">合同管理</a></li>
            <li class="active"><a href="#">合同编辑</a></li>
        </ol>
    </section>
    <section class="content">
    <form id="submitForm" role="form" class="form-horizontal" action="${ctx}/contract/saveEditContract.html">
    	<div class=" box box-success">    			
    		      <div class="box-body">
                 	<div class="form-group form-group-xs">
                        <label for="exampleInputEmail1" class="col-sm-2 text-right">合同名称：</label>
                        <div class="col-sm-9 text-left">
                            <input class="form-control" name="contractTitle" id="contractTitle" placeholder="合同名称" type="text" maxlength="200" value="${contract.contractTitle}">
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                   		  <label for="version" class="col-sm-2 text-right">合同甲方：</label>
                   		  <div class="col-sm-8 text-left" id="partyA">
			                  <input id="partyAName" name="partyAName" class="form-control" readonly="readonly" placeholder="合同甲方" disabled="disabled"  type="text" value="${partyAName }" />
		                  </div>
		                  <div class="col-sm-1 text-left" > 
		                      <input id="partyAId" name="partyAId" type="hidden" value="${partyA.contractParty }"/>
		                      <a id="partyChoose" href="javascript:void(0)" onclick="selectParty(this.id);" class="btn btn-default btn-sm" >选择</a>
	                      </div>
		            </div>
		            
		            <div class="form-group form-group-xs" >
                   		  <label for="version" class="col-sm-2 text-right">合同乙方：</label>
                   		  <div class="col-sm-8 text-left" id="partyB">
		                  <input id="partyBName" name="partyBName" class="form-control" readonly="readonly" placeholder="合同甲方" disabled="disabled"  type="text" value="${partyBName }"/>
	                      </div>
	                      <div class="col-sm-1 text-left" > 
	                      <input id="partyBId" name="partyBId" type="hidden" value="${partyB.contractParty }"/>
	                      <a id="partyChoose1" href="javascript:void(0)" onclick="selectParty(this.id);" class="btn btn-default btn-sm">选择</a>
	                      </div>
		            </div>
		            
		            <div class="form-group form-group-xs" >
                   		  <label for="version" class="col-sm-2 text-right">合同药品：</label>
                   		  <div class="col-sm-8 text-left">
                   		  <ol style="list-style-type:none" id="drugs">
                   		  	<li id="drug0" style="margin-left:0px">
	                   		    <i class="fa fa-plus" id="drugs0plus" style="color: green;float:left" onclick="addDrug(this.id)"></i>
	                			<input id="drugs0Name" class="form-control" readonly="readonly" placeholder="药品" disabled="disabled"  type="text"></input>
	                			<input id="drugs0Id" type="hidden" value=""/>
	                			<!--  --><a id="drugs0select" href="javascript:void(0)" onclick="selectDrug(this.id);" class="btn btn-default btn-sm" >选择</a>
                   		 	</li>  
	                      </ol>
	                      </div>
		            </div>
		            
                    <div class="form-group form-group-xs">
                        <label for="inputTextAlter4" class="col-sm-2 text-right">合同内容：</label>
                        <div class="col-sm-9 text-left">
                            <div id="editor" style="height:400px;" name="content">
                            </div>
                        </div>
                    </div>
                    <input type="hidden" value="${contract.contractId}" id="contractId" name="contractId">
                     <input type="hidden" value="" id="partyAndDrugData" name="partyAndDrugData">
                    <input type="hidden" value="" id="txtContent" name="txtContent">
                    <!-- 保存项目公告id -->
                    <input type="hidden" value="${contractModel.contractModelId}" id="contractModelId" name="contractModelId">
                </div>
                <div class="box-footer text-center">
                    <a onclick ="save()" class="btn btn-success btn-sm">保&nbsp;存</a>
                    <a onclick ="back()" class="btn btn-danger btn-sm">返&nbsp;回</a>
                </div>
                
        	</div>
        </form>	
    </section>
     <%@ include file="/WEB-INF/component/commonJS.jsp" %>  
     <script src="${ctx}/lib/plugins/ueditor/lang/zh-cn/zh-cn.js"></script>
    <script type="text/javascript">
        
	    function selectDrug(strDrugId){
	    	var strDrugId = $("#"+strDrugId).parent().attr("id");
	       if(strDrugId != "" && null != strDrugId){
	  		   $.HN.dialog.opendialog(1,"药品列表","${ctx}/contract/toSelectContractDrug.html?strDrugId="+strDrugId,"800px","400px",operDrugResult);
	  	   }else{
	  		   $.HN.dialog.opendialog(1,"药品列表","${ctx}/contract/toSelectContractDrug.html","800px","400px",operDrugResult);
	  	   }
	    }
	    
	    function selectParty(strPartyId){
	    	var strPartyId = $("#"+strPartyId).parent().attr('id');
	    	if(strPartyId!="" && null!=strPartyId){
	    	   $.HN.dialog.opendialog(1,"生产企业列表","${ctx}/contract/toSelectContractParty.html?strPartyId="+strPartyId,"800px","400px",operCompResult);
	    	}else{
	    	   $.HN.dialog.opendialog(1,"生产企业列表","${ctx}/contract/toSelectContractParty.html","800px","400px",operCompResult);
	    	}
	    }
	    
	    function operCompResult(val){
            var str = val.split("|$");
            var partyName = str[1];
            var partyId = str[0];
            var partyHtmlId = str[2];
            $("#"+partyHtmlId).children("input[type='hidden']").val(partyId);
            $("#"+partyHtmlId).children("input[type='text']").val(partyName);
            
        }
	    
	    function operDrugResult(val){
       	 var str = val.split("|$");
            var drugId = str[0];
            var drugName = str[1];
            var drugHtmlId = str[2];
            $("#"+drugHtmlId).children("input[type='hidden']").val(drugId);
            $("#"+drugHtmlId).children("input[type='text']").val(drugName);
        }
        var ue;
        function save(){
        	if(!validate()){
        		return;
        	}
    		$.HN.message.confirm('确定要保存吗？', '', '').on(function (e) {
                   if (e) {
                var ue = UE.getEditor('editor');
                var txtContent = ue.getContentTxt();
                $("#txtContent").val(txtContent)
                getPartyAndDrugData();
    			$("#submitForm").ajaxSubmit({
    				 type: 'post',
    				dataType : "json",
    				timeout: 10000,
    				success : function(result) {
    					if (result.success) {
    						$.HN.message.alert("保存成功！", "消息", "success", function() {
    							window.location.href="${ctx}/contract/toBidContractList.html";
    						});
    					} else {
    						$.HN.message.alert(result.msg || "", "消息", "error");
    					}
    				}
    			});
             }
           });
        }
        function getPartyAndDrugData(){
        	var children = $("#partys").children("li");
        	var jsonString = "{";
        	var partyAId = $("#partyAId").val();
        	var partyBId = $("#partyBId").val();
        	var drugLi = $("#drugs").children("li");
        	var drugStr = "";
        	for(var i=0; i< drugLi.length;i++){
        		var drug =  drugLi.eq(i);
        		var drugId = drug.children("input[type='hidden']").val();
        		drugStr = drugStr + drugId+",";
        	}
        	drugStr = drugStr.substring(0,drugStr.length-1);
        	jsonString = jsonString + "'partyAId':'"+partyAId+"',";
        	jsonString = jsonString + "'partyBId':'"+partyBId+"',";
        	jsonString = jsonString +"'drugIds':'"+drugStr+"'";
        	jsonString = jsonString+"}"
            $("#partyAndDrugData").val(jsonString);
        	return jsonString;
        }     
        function validate(){
        	var contractTitle = $.trim($("#contractTitle").val());
    		if (contractTitle == null || contractTitle == "") {
    			$.HN.message.alert("合同名称不能为空！", "消息", "info");
    			return false;
    		} 
    		return true;
        }
        
        function back(){
        	window.location.href="${ctx}/contract/toBidContractList.html";
        }
        
        function addDrug(id){
        	var drugNum = $("#drugs").data("drugNum");
        	$("#"+id).attr({ "class": "fa fa-times", "onclick": "removeDiv(this.id)","style":"color: red; margin-right: 15px; float:left" });
        	$("#drugs").append( "<li id='drug"+drugNum+"' style='margin-left:0px'>"
                	+ "<i class='fa fa-plus' id='drugs"+drugNum+"plus' style='color: green;margin-right: 15px;margin-bottom:5px; float:left' onclick='addDrug(this.id)'></i>"
                	+ "<input id='drugs"+drugNum+"Name' class='form-control' readonly='readonly' placeholder='药品' disabled='disabled'  type='text' style='float:left;width:400px;'></input>"
                	+ "<input id='drugs"+drugNum+"Id' type='hidden' value=''/>"
                	+ "<a id='drugs"+drugNum+"select' href='javascript:void(0)' onclick='selectDrug(this.id);' class='btn btn-default btn-sm' style='margin-left:10px'>选择</a>"
                    + "</li>");  
        	$("#drugs").data("drugNum",drugNum+1);//元素个数+1
        }
        
        function removeDiv(id){
        	$("#"+id).parent().remove();
        }
		$(function() {
			var jsonO = eval('${jsonDrugInfo}');
			var childElemenNum = $("#drugs").children("li").length;
			$("#drugs").data("drugNum",1);//将数据存储在元素中
			//编辑药品信息
			if(jsonO.length!=0){
				$("#drugs").data("drugNum",0)
				$("#drugs").html("");
			}
			for(var i = 0; i < jsonO.length; i++){
				$("#drugs").append( "<li id='drug"+i+"' style='margin-left:0px'>"
            	+ "<i class='fa fa-plus' id='drugs"+i+"plus' style='color: green;margin-right: 15px; float:left' onclick='addDrug(this.id)'></i>"
            	+ "<input id='drugs"+i+"Name' class='form-control' readonly='readonly' placeholder='药品' disabled='disabled'  type='text' style='float:left;width:400px;'></input>"
            	+ "<input id='drugs"+i+"Id' type='hidden' value='"+jsonO[i].drugId+"'/>"
            	+ "<a id='drugs"+i+"select' href='javascript:void(0)' onclick='selectDrug(this.id);' class='btn btn-default btn-sm' style='margin-left:10px'>选择</a>"
                + "</li>")
                if(i !=jsonO.length-1){
                	$("#drugs"+i+"plus").attr({ "class": "fa fa-times", "onclick": "removeDiv(this.id)","style":"color: red; margin-right: 15px; float:left"  });
                }
                $("#drugs"+i+"Name").val(jsonO[i].name);
				$("#drugs"+i+"Id").val(jsonO[i].drugId);
				var drugNum = $("#drugs").data("drugNum");
				$("#drugs").data("drugNum",drugNum+1);//元素个数+1
             }
		     ue = UE.getEditor('editor');
		     var optType = "${optType}";
			 ue.ready(function() {
		         var content="${content}";
		         ue.setContent(content);
		   	});
		});
    </script>
</body>
</html>
